<!-- 广告效果报表 -->
<template>
  <div>
    <h2>广告效果报表</h2>

    <el-button type="primary" @click="ToggleEvent">
      {{ toggleView ? '显示广告点击' : '显示消耗成本' }}
    </el-button>

    <el-table v-if="!toggleView" :data=adPerformanceDaily style="width: 100%" border stripe v-loading="loading">
      <el-table-column prop="id" label="编号" align="center"></el-table-column>
      <el-table-column prop="companyName" label="投告公司名称" width="180" align="center"></el-table-column>
      <el-table-column prop="campaignName" label="广告计划" align="center"></el-table-column>
      <el-table-column prop="title" label="广告类型" align="center"></el-table-column>
      <el-table-column prop="description" label="广告名称" align="center"></el-table-column>
      <el-table-column prop="impressionCount" label="阅读量" align="center"></el-table-column>
      <el-table-column prop="clickCount" label="访客数" align="center"></el-table-column>
      <el-table-column prop="conversionCount" label="评论数" align="center"></el-table-column>

      <el-table-column prop="statDate" label="统计日期" align="center">
        <template #default="{ row }">
          {{ formatDate(row.statDate) }}
        </template>
      </el-table-column>
    </el-table>

    <el-table v-else :data=adPerformanceDaily style="width: 100%" border stripe v-loading="loading">
      <el-table-column prop="id" label="编号" align="center"></el-table-column>
      <el-table-column prop="companyName" label="投告公司名称" width="180" align="center"></el-table-column>
      <el-table-column prop="description" label="广告名称" align="center"></el-table-column>
      <el-table-column prop="ctr" label="点击率(%)" align="center">
        <template #default="{ row }">
          {{ row.ctr }}%
        </template>
      </el-table-column>
      <el-table-column prop="cvr" label="转化率(%)" align="center">
        <template #default="{ row }">
          {{ row.cvr }}%
        </template>
      </el-table-column>
      <el-table-column prop="costAmount" label="消耗金额" align="center">
        <template #default="{ row }">
          {{ row.costAmount }}w
        </template>
      </el-table-column>
      <el-table-column prop="cpc" label="平均点击成本" align="center">
        <template #default="{ row }">
          {{ row.cpc }}w
        </template>
      </el-table-column>
      <el-table-column prop="cpm" label="千次展示成本" align="center">
        <template #default="{ row }">
          {{ row.cpm }}w
        </template>
      </el-table-column>
      <el-table-column prop="cpa" label="平均转化成本" align="center">
        <template #default="{ row }">
          {{ row.cpa }}w
        </template>
      </el-table-column>
      <el-table-column prop="createDate" label="创建时间" align="center">
        <template #default="{ row }">
          {{ formatDate(row.statDate) }}
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加分页组件 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handlePageChange" :current-page="currentPage"
      :page-sizes="[6, 10, 20, 50]" :page-size="pageSize" layout="prev, pager, next,total, sizes " :total="totalCount">
    </el-pagination>
  </div>
</template>

<script>
import axios from '@/utils/request';

export default {
  // name: 'AdEffectReport',
  name: 'AdPerformanceDaily',
  data() {
    return {
      loading: false,
      adPerformanceDaily: [],
      toggleView: false,
      currentPage: 1,
      pageSize: 6,
      totalCount: 0
    }
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      if (!this.toggleView) {
        this.fetchDailyData();
      } else {
        this.fetchSummaryData();
      }
    },
    fetchDailyData() {
      this.loading = true;
      const url = '/adPerformanceDaily/daily/page';
      const params = { current: this.currentPage, size: this.pageSize }
      axios.get(url, { params }).then(response => {
        if (response.data && response.data.code === 200) {
          this.adPerformanceDaily = response.data.data.records;
          this.totalCount = response.data.data.total;
          if (this.adPerformanceDaily.length === 0) {
            this.$message.warning('加载数据失败');
          }
        } else {
          this.$message.error(response.data?.message || '获取行为统计失败');
        }
      }).catch(error => {
        console.error('获取行为统计失败:', error);
        this.$message.error('获取行为统计失败: ' + (error.response?.data?.message || error.message));
      }).finally(() => {
        this.loading = false;
      });
    },
    fetchSummaryData() {
      const url = '/adPerformanceDaily/daily/page';
      const params = { current: this.currentPage, size: this.pageSize }
      axios.get(url, { params }).then(response => {
        if (response.data && response.data.code === 200) {
          this.adPerformanceDaily = response.data.data.records;
          this.totalCount = response.data.data.total;
        }
      })
    },
    ToggleEvent() {
      this.toggleView = !this.toggleView;
      this.currentPage = 1;
      this.fetchData();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.fetchData();
    },
    handlePageChange(val) {
      this.currentPage = val;
      this.fetchData();
    },
    formatDate(date) {
      if (!date) return '';
      try {
        const dateObj = typeof date === 'string' ? new Date(date) : date;
        const year = dateObj.getFullYear();
        const month = String(dateObj.getMonth() + 1).padStart(2, '0');
        const day = String(dateObj.getDate()).padStart(2, '0');
        return `${year}-${month}-${day}`;
      } catch (e) {
        return date;
      }
    }
  },
};  
</script>

<style scoped>
/* Add any CSS styles you need for this page here */
</style>